#include "base/core/gamemath.h"
#include "Path.h"


Path::Path()
{
	m_path.clear();
	m_totalPathLength = 0.0f;
	m_nextIndex = 1;
}

Path::~Path()
{
}
bool Path::Init(const ActorSkillMove &moveData)
{
	if (moveData.path_size() <= 0)
		return false;
	m_path.clear();
	m_nextIndex = 1;
	m_totalPathLength = 0;

	Point3<float> beginP(moveData.currentpos().x(), moveData.currentpos().y(), moveData.currentpos().z());
	Point3<float> endP;
	float curDis = 0;

	//这里是技能移动的，不知道这里的当前坐标是否已经包含在路径中，暂时先不去掉
	m_path.push_back(beginP);
	//检验路径后面做
	for (int32_t i = 0; i < moveData.path_size(); i++)
	{
		endP.x = moveData.path(i).x();
		endP.y = moveData.path(i).y();
		endP.z = moveData.path(i).z();

		//先判断长度是否合适
		curDis = point3Length(beginP, endP);
		curDis = curDis < 0.0f ? 0.0f : curDis;
		m_totalPathLength += curDis;
		m_path.push_back(endP);
		beginP = endP;
	}

	return true;
}

bool Path::Init(const ActorMove &moveData)
{
	if (moveData.path_size() <= 0)
		return false;

	m_path.clear();
	m_nextIndex = 1;
	m_totalPathLength = 0;

	Point3<float> beginP(moveData.currentpos().x(), moveData.currentpos().y(), moveData.currentpos().z());
	Point3<float> endP;
	float curDis = 0;

	//当前点已经包含在路径中，这里不用再加入当前坐标
	//m_path.push_back(beginP);
	//检验路径后面做
	for (int32_t i = 0; i < moveData.path_size(); i++)
	{
		endP.x = moveData.path(i).x();
		endP.y = moveData.path(i).y();
		endP.z = moveData.path(i).z();

		//先判断长度是否合适
		curDis = point3Length(beginP, endP);
		curDis = curDis < 0.0f ? 0.0f : curDis;
		m_totalPathLength += curDis;
		m_path.push_back(endP);
		beginP = endP;
	}
	//LogDebugFmtPrint("end point(%f,%f,%f)", endP.x, endP.y, endP.z);
	return true;
}

bool Path::Init(VEC_POINT3& path, Point3<float>& curPos)
{
	if (path.size() <= 0)
		return false;
	m_path.clear();
	m_nextIndex = 1;
	m_totalPathLength = 0;

	Point3<float> beginP(curPos.x, curPos.y, curPos.z);
	Point3<float> endP;
	float curDis = 0;

	//当前点已经包含在路径中，这里不用再加入当前坐标
	//m_path.push_back(beginP);
	//检验路径后面做
	for (uint32_t i = 0; i < path.size(); i++)
	{
		endP.x = path[i].x;
		endP.y = path[i].y;
		endP.z = path[i].z;

		//先判断长度是否合适
		curDis = point3Length(beginP, endP);
		curDis = curDis < 0.0f ? 0.0f : curDis;
		m_totalPathLength += curDis;
		m_path.push_back(endP);
		beginP = endP;
	}
	return true;
}

bool Path::GetLeftPath(VEC_POINT3 & leftPath)
{
	for (uint32_t i = m_nextIndex; i < (uint32_t)m_path.size(); ++i)
	{
		leftPath.push_back(m_path[i]);
	}
	return true;
}

void Path::UnInit()
{
}

bool Path::MapPathDistanceToPoint(float setpDistance, Point3<float>& retPos)
{
	float remaining = setpDistance;

	/*
	if (setpDistance >= m_totalPathLength)	//如果距离大于路径剩余的距离，则返回路径的最后一个点
	{
		retPos = m_path[m_path.size() - 1];
		return true;
	}
	*/

	float segmentLen = 0.0f;
	float ratio = 0.0f;

	for (; m_nextIndex < m_path.size(); m_nextIndex++)
	{
		//Point3<float> vtmp = m_path[m_nextIndex] - m_path[m_nextIndex - 1];
		//segmentLen = sqrt(vtmp.x*vtmp.x + vtmp.z*vtmp.z); 
		segmentLen = point3Length(m_path[m_nextIndex], m_path[m_nextIndex - 1]);
		if (segmentLen < remaining)
		{
			remaining -= segmentLen;
			retPos = m_path[m_nextIndex];
		}
		else 
		{
			ratio  = remaining / segmentLen;
			retPos = interpolate(ratio, m_path[m_nextIndex - 1], m_path[m_nextIndex]);
			m_path[m_nextIndex - 1] = retPos;
			break;
		}
	}
	if (m_nextIndex == m_path.size())
		return true;
	
	return false;
}

void Path::GetMsgPath(ActorMove &moveData)
{
	Vector3PB *path = nullptr;
	for (uint32_t i = m_nextIndex; i < m_path.size(); i++)
	{
		path = moveData.add_path();
		path->set_x(m_path[i].x);
		path->set_y(m_path[i].y);
		path->set_z(m_path[i].z);
	}
}
